import axios from 'axios'

const http = axios.create({
  // baseURL 留空，使用 Vite 代理 '/api' 到后端
  timeout: 15000,
})

http.interceptors.response.use(
  (response) => {
    const data = response.data
    if (data && typeof data === 'object' && 'code' in data) {
      if (data.code === 0) {
        return data.data
      }
      const error = new Error(data.message || '请求失败')
      error.code = data.code
      throw error
    }
    return data
  },
  (error) => {
    const message = error?.response?.data?.message || error.message || '网络错误'
    return Promise.reject(new Error(message))
  }
)

export default http

